﻿using AutoMapper;
using MediatR;
using System.Security.Cryptography.Xml;
using T.S.BaseData.Api.AppClinet.Commend.Imgs;
using T.S.BaseData.Api.Dto.Img;
using T.S.BaseData.Domain.Entity;
using T.S.BaseData.ErrorCode;
using T.S.BaseData.Infrastructure.Base;

namespace T.S.BaseData.Api.AppClinet.CommendHand.Imgs
{
    public class ImgGetCommandHandle : IRequestHandler<ImgGetCommand, ApiResult<List<GetImgDto>>>
    {

        private readonly IBaseRepository<Img> imgRepo;
        private readonly IMapper mapper;

        public ImgGetCommandHandle(IBaseRepository<Img> imgRepo, 
            IMapper mapper)
        {
            this.imgRepo = imgRepo;
            this.mapper = mapper;
        }

        public Task<ApiResult<List<GetImgDto>>> Handle(ImgGetCommand request, CancellationToken cancellationToken)
        {

            ApiResult<List<GetImgDto>> result = new ApiResult<List<GetImgDto>>();

            var img = imgRepo.GetAll().Where(x => x.GoodsId == request.GoodsId && x.ImgType == request.ImgType && !x.IsDelete);

            var model = mapper.Map<List<GetImgDto>>(img);

            result.Code = ApiEnums.Success;
            result.Data = model;
            result.Meg = "";

            return Task.FromResult(result);
        }
    }
}
